package com.xiaobai.service;

import com.alibaba.fastjson.JSON;
import com.xiaobai.dao.StudentCourseDao;
import com.xiaobai.pojo.StudentCourse;
import com.xiaobai.vo.AllCourseAllList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UnitsCourseService {

    @Autowired
    private StudentCourseDao studentCourseDao;

    /**
     * 第一次登录,两个库都插入
     * @param allCourseAllList
     * @param student_id
     */
//    @Async("getAsyncExecutor")//为了事务,不加注释了
    public void saveCourse(AllCourseAllList allCourseAllList, String student_id){

        /**
         * 改动,改成批量插入
         */

        List<StudentCourse> list=mergeAllList(allCourseAllList,student_id);

        //开始批量插入
        studentCourseDao.insertBasicPiLiangChaRuCourse(list);//插入备份
        studentCourseDao.insertPiLiangChaRuCourse(list);//插入自定义
    }

    /**
     * 仅更新备份库
     * @param allCourseAllList
     * @param student_id
     */
    @Async("getAsyncExecutor")
    public void updateCourse2(AllCourseAllList allCourseAllList, String student_id){

        List<StudentCourse> list=mergeAllList(allCourseAllList, student_id);

        studentCourseDao.insertBasicPiLiangChaRuCourse(list);//插入备份
    }

    /**
     * 更新自定义库
     * @param allCourseAllList
     * @param student_id
     */
    @Async("getAsyncExecutor")
    public void updateCourse3(AllCourseAllList allCourseAllList, String student_id){

        List<StudentCourse> list=mergeAllList(allCourseAllList, student_id);

        studentCourseDao.insertPiLiangChaRuCourse(list);//插入自定义

    }

    //转换方法
    private List<StudentCourse> mergeAllList(AllCourseAllList allCourseAllList,String student_id){

        List<StudentCourse> list=new ArrayList<>();

        for(int i=1;i<=20;i++){
            switch (i){
                case 1:
                    StudentCourse studentCourse1=new StudentCourse();
                    studentCourse1.setStudentId(student_id);
                    studentCourse1.setWeek(i);
                    studentCourse1.setConnect(JSON.toJSONString(allCourseAllList.getOneCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse1);
                    break;
                case 2:
                    StudentCourse studentCourse2=new StudentCourse();
                    studentCourse2.setStudentId(student_id);
                    studentCourse2.setWeek(i);
                    studentCourse2.setConnect(JSON.toJSONString(allCourseAllList.getTwoCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse2);
                    break;
                case 3:
                    StudentCourse studentCourse3=new StudentCourse();
                    studentCourse3.setStudentId(student_id);
                    studentCourse3.setWeek(i);
                    studentCourse3.setConnect(JSON.toJSONString(allCourseAllList.getThreeCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse3);
                    break;
                case 4:
                    StudentCourse studentCourse4=new StudentCourse();
                    studentCourse4.setStudentId(student_id);
                    studentCourse4.setWeek(i);
                    studentCourse4.setConnect(JSON.toJSONString(allCourseAllList.getFourCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse4);
                    break;
                case 5:
                    StudentCourse studentCourse5=new StudentCourse();
                    studentCourse5.setStudentId(student_id);
                    studentCourse5.setWeek(i);
                    studentCourse5.setConnect(JSON.toJSONString(allCourseAllList.getFiveCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse5);
                    break;
                case 6:
                    StudentCourse studentCourse6=new StudentCourse();
                    studentCourse6.setStudentId(student_id);
                    studentCourse6.setWeek(i);
                    studentCourse6.setConnect(JSON.toJSONString(allCourseAllList.getSixCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse6);
                    break;
                case 7:
                    StudentCourse studentCourse7=new StudentCourse();
                    studentCourse7.setStudentId(student_id);
                    studentCourse7.setWeek(i);
                    studentCourse7.setConnect(JSON.toJSONString(allCourseAllList.getSevenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse7);
                    break;
                case 8:
                    StudentCourse studentCourse8=new StudentCourse();
                    studentCourse8.setStudentId(student_id);
                    studentCourse8.setWeek(i);
                    studentCourse8.setConnect(JSON.toJSONString(allCourseAllList.getEigCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse8);
                    break;
                case 9:
                    StudentCourse studentCourse9=new StudentCourse();
                    studentCourse9.setStudentId(student_id);
                    studentCourse9.setWeek(i);
                    studentCourse9.setConnect(JSON.toJSONString(allCourseAllList.getNineCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse9);
                    break;
                case 10:
                    StudentCourse studentCourse10=new StudentCourse();
                    studentCourse10.setStudentId(student_id);
                    studentCourse10.setWeek(i);
                    studentCourse10.setConnect(JSON.toJSONString(allCourseAllList.getTenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse10);
                    break;
                case 11:
                    StudentCourse studentCourse11=new StudentCourse();
                    studentCourse11.setStudentId(student_id);
                    studentCourse11.setWeek(i);
                    studentCourse11.setConnect(JSON.toJSONString(allCourseAllList.getElevenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse11);
                    break;
                case 12:
                    StudentCourse studentCourse12=new StudentCourse();
                    studentCourse12.setStudentId(student_id);
                    studentCourse12.setWeek(i);
                    studentCourse12.setConnect(JSON.toJSONString(allCourseAllList.getTwelveCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse12);
                    break;
                case 13:
                    StudentCourse studentCourse13=new StudentCourse();
                    studentCourse13.setStudentId(student_id);
                    studentCourse13.setWeek(i);
                    studentCourse13.setConnect(JSON.toJSONString(allCourseAllList.getThirteenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse13);
                    break;
                case 14:
                    StudentCourse studentCourse14=new StudentCourse();
                    studentCourse14.setStudentId(student_id);
                    studentCourse14.setWeek(i);
                    studentCourse14.setConnect(JSON.toJSONString(allCourseAllList.getFourteenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse14);
                    break;
                case 15:
                    StudentCourse studentCourse15=new StudentCourse();
                    studentCourse15.setStudentId(student_id);
                    studentCourse15.setWeek(i);
                    studentCourse15.setConnect(JSON.toJSONString(allCourseAllList.getFifteenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse15);
                    break;
                case 16:
                    StudentCourse studentCourse16=new StudentCourse();
                    studentCourse16.setStudentId(student_id);
                    studentCourse16.setWeek(i);
                    studentCourse16.setConnect(JSON.toJSONString(allCourseAllList.getSixteenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse16);
                    break;
                case 17:
                    StudentCourse studentCourse17=new StudentCourse();
                    studentCourse17.setStudentId(student_id);
                    studentCourse17.setWeek(i);
                    studentCourse17.setConnect(JSON.toJSONString(allCourseAllList.getSeventeenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse17);
                    break;
                case 18:
                    StudentCourse studentCourse18=new StudentCourse();
                    studentCourse18.setStudentId(student_id);
                    studentCourse18.setWeek(i);
                    studentCourse18.setConnect(JSON.toJSONString(allCourseAllList.getEighteenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse18);
                    break;
                case 19:
                    StudentCourse studentCourse19=new StudentCourse();
                    studentCourse19.setStudentId(student_id);
                    studentCourse19.setWeek(i);
                    studentCourse19.setConnect(JSON.toJSONString(allCourseAllList.getNineteenCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse19);
                    break;
                case 20:
                    StudentCourse studentCourse20=new StudentCourse();
                    studentCourse20.setStudentId(student_id);
                    studentCourse20.setWeek(i);
                    studentCourse20.setConnect(JSON.toJSONString(allCourseAllList.getTwentyCourseWeekList().getIndexCourseList()));

                    list.add(studentCourse20);
                    break;
            }
        }

        return list;

    }

}
